package com.sanen.flyer.entity.script;

import com.sanen.flyer.common.Animation;
import com.sanen.flyer.common.Constant;
import com.sanen.flyer.entity.FlyObject;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 敌机精灵
 * @author wsse
 * @date 2018-09-19 16:46
 */
public class EmptySprit {
    public static final int STATE_NORMAL = 0;
    public static final int STATE_BY_HIT = 1;
    BufferedImage img;
    Animation byHitAnim;
    int state;
    FlyObject fly;
    int width;//图像宽度
    int height;//图像高度
    int imgWidth;//图片宽度
    int imgHeight;//图片高度
    int playCount=2;
    public EmptySprit(FlyObject fly, BufferedImage img) {
        this.fly = fly;
        this.img = img;
        this.imgWidth = (int) (img.getWidth() * Constant.getRatio());
        this.imgHeight = (int) (img.getHeight() * Constant.getRatio());
        this.height = imgHeight;
        this.width = this.imgWidth / 2;
        byHitAnim = new Animation(fly, img, 2, 60, playCount);
    }

    public void draw(Graphics g) {
        switch (state) {
            case STATE_NORMAL://默认状态
                drawNormal(g);
                break;
            case STATE_BY_HIT://被攻击状态
                byHitAnim.draw(g);
                break;
        }
//        攻击动画播放完毕,切回默认状态
        if (byHitAnim.getPlayCount() == 0) state = STATE_NORMAL;
    }

    private void drawNormal(Graphics g) {
        g.setClip(fly.getX(), fly.getY(), width, height);
        g.drawImage(img, fly.getX(), fly.getY(), imgWidth, imgHeight, null);
    }
    public void byHit(){
        state=STATE_BY_HIT;
        byHitAnim.setPlayCount(playCount);
    }
}
